import java.util.ArrayList;
import java.util.List;

/**
 * @program: LeetCode
 * @ClassName Num032_2_levelOrder
 * @description: LeetCode : 剑指 Offer 32 - II. 从上到下打印二叉树 II
 * @author: WXY
 * @create: 2022-12-09 14:18
 * @Version 1.0
 **/


public class Num032_2_levelOrder {
    public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode(int x) { val = x; }
 }

    public static List<List<Integer>> list = new ArrayList<>();
    public static List<List<Integer>> levelOrder(TreeNode root) {
        process(root, 0);
        return list;
    }

    private static void process(TreeNode root, int k) {
        if (root != null) {
            if (list.size() <= k) list.add(new ArrayList<>());
            list.get(k).add(root.val);
            process(root.left, k + 1);
            process(root.right, k + 1);
        }
    }
}
